<?php
namespace Nawa\Survey\Question;
/**
 * Question Type
 *
 * It represant a type of question
 *
 * @author Xavier VILAIN
 * @package Nawa\Survey\Question
 * @license
 *
 * @property int	$type			Type id (4)
 * @property string	$label			Label
 * @property bool	$standard		Is a standard type (can't be deleted)
 * @property bool	$active			Is an active type (can be selected)
 * @property string	$reference		Fox\Html\Object Reference
 */
class Type extends \Fox\Object {

	/**
	 * Default Answers param
	 * @var string
	 */
	const DEFAULT_ANSWERS = 'anwsers';
	
	/**
	 * To standardize the creation/update
	 * @return string label
	 */
	public function label() {
		return $this->label;
	}

	/**
	 * Json Decoded Params
	 * @var array
	 */
	private $_decodedParams = null;
	
	/**
	 * Renderer Object
	 * @var mixed
	 */
	private $_renderer = null;
	
	/**
	 * Decode Params
	 * @return array
	 */
	public function decodeParams() {
		
		if(empty($this->_decodedParams))
		{
			$this->_decodedParams = json_decode($this->params,true);
			if(!is_array($this->_decodedParams)) {
				$this->_decodedParams= array();
			}
		}
		return $this->_decodedParams;
	}
	
	/**
	 * 
	 * Get a specified param values
	 * @param string $param
	 * @return mixed
	 */
	public function param($param) {
		$params = $this->decodeParams();
		if(!empty($params[$param])) {
			return $params[$param];
		} else {
			return null;
		}
	}
	
	/**
	 * Return associate type answers renderer
	 * @return Question\Renderer
	 * 
	 */
	public function renderer() {
		if(empty($this->_renderer)) {
			$rendererClassName = $this->reference;
			$this->_renderer = new $rendererClassName;
		}
		return $this->_renderer;
	}
	
	/**
	 * Return associate type answers renderer
	 * @return Question\Renderer
	 * 
	 */
	public function renderAsPDF(&$pdf) {
		if(empty($this->_renderer)) {
			$rendererClassName = $this->reference;
			$rendererClassName = str_replace('Renderer','Renderer\Pdf',$rendererClassName);
			$this->_renderer = new $rendererClassName;
			$this->_renderer->setPdf($pdf);
		}
		return $this->_renderer;
	}
}
\Fox\Dao::infos(Type::className(),
new \Fox\Object\Info('survey_question_types', \Fox\Databases::DB_APP)
);